package com.iplay.实战java高并发程序设计.chapter5.c54;

import com.lmax.disruptor.RingBuffer;

import java.nio.ByteBuffer;

/**
 * @author dangwu
 * @since 2021/11/5 3:22 下午
 */
public class Producer {
    private final RingBuffer<PCData> ringBuffer;
    public Producer(RingBuffer<PCData> ringBuffer){
        this.ringBuffer = ringBuffer;
    }
    //在ByteBuffer对象中可以用来包装任何数据类型
    public void pushData(ByteBuffer bb){
        long sequence = ringBuffer.next();
        try {
            //取得下一个空闲可用的PCData对象，并且将PCData对象的数据设为期望值
            PCData event = ringBuffer.get(sequence);
            event.setValue(bb.getLong(0));
        }finally {
            ringBuffer.publish(sequence);
        }
    }
}
